package com.eshop.modules.system.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.modules.system.service.dto.DictDetailDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.system.service.dto.DictDetailQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.system.service.DictDetailService;
import com.eshop.modules.system.domain.DictDetail;
import com.eshop.modules.system.service.mapper.DictDetailMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class DictDetailServiceImpl extends BaseServiceImpl<DictDetailMapper, DictDetail> implements DictDetailService
{
    private final IGenerator generator;
    
    public Map<String, Object> queryAll(final DictDetailQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<DictDetail> page = (PageInfo<DictDetail>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)DictDetailDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<DictDetail> queryAll(final DictDetailQueryCriteria criteria) {
        final List<DictDetail> list = ((DictDetailMapper)this.baseMapper).selectDictDetailList(criteria.getLabel(), criteria.getDictName());
        return list;
    }
    
    public void download(final List<DictDetailDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final DictDetailDto dictDetail : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("字典标签", dictDetail.getLabel());
            map.put("字典值", dictDetail.getValue());
            map.put("排序", dictDetail.getSort());
            map.put("字典id", dictDetail.getDictId());
            map.put("创建日期", dictDetail.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public DictDetailServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
